package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.EvaluationDetailBo;
import com.tpyule.tpclass.entity.dto.EvaluationQueryDto;
import com.tpyule.tpclass.entity.pojo.Evaluation;
import com.tpyule.tpclass.service.EvaluationService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/18 19:46
 */
@RestController
@RequestMapping("/evaluation")
public class EvaluationController extends BaseController {
    @Autowired
    EvaluationService evaluationService;

    @RequestMapping("/add")
    @ResponseBody
    public void addEvaluation(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String evaluationStr = request.getParameter("evaluationData");
            if(evaluationStr!=null){
                Evaluation evaluation = objectMapper.readValue(evaluationStr, Evaluation.class);
                int result = evaluationService.addEvaluation(evaluation);
                if(result>0){
                    resultBean = new SuccessResultBean("添加成功");
                }else {
                    resultBean = new FailResultBean("添加失败，请重试");
                }
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }

    }

    @RequestMapping("/getEvaluation")
    @ResponseBody
    public void getEvaluations(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String evQueryStr = request.getParameter("evQueryData");
            if(evQueryStr!=null){
                EvaluationQueryDto evaluationQueryDto = objectMapper.readValue(evQueryStr, EvaluationQueryDto.class);
                List<EvaluationDetailBo> evaluations = evaluationService.getEvaluations(evaluationQueryDto);
                if(evaluations!=null){
                    resultBean = new SuccessResultBean(evaluations,"获取成功");
                }else {
                    resultBean = new FailResultBean("获取失败，请重试");
                }
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }

    }
}
